package com.neu.service.snn;

import com.neu.dao.snn.UserDaoImpl;

import java.util.List;
import java.util.Map;

public class UserService {
    private UserDaoImpl userDao;
    private int row;

    public UserService(){
        userDao = new UserDaoImpl();
    }
    //保存用户头像
    public boolean doSave(String u_img,String u_id){
        boolean flag = false;
        try {
            int row = userDao.save(u_img,u_id);
            if (row>0){
                flag=true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
    //查看已付款和交易完成的订单
    public List<Map<String,Object>> findAllById(String u_id){
        List<Map<String,Object>> list=null;
        try {
            list=userDao.findAllById(u_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    //查询用户的消费情况
    public List<Map<String,Object>> findById(String u_id){
        List<Map<String,Object>> list=null;
        try {
            list=userDao.findById(u_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    //修改用户信息
    public  boolean editUserInfo(String u_name,String u_email,String u_sex,String u_birth,String u_id){
        boolean flag = false;
        try {
            int row = userDao.editUserInfo(u_name,u_email,u_sex,u_birth,u_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  flag;
    }
    //修改用户密码
    public boolean editPwd(String u_id,String u_pwd,String u_pwd1){
        boolean flag = false;
        try {
            int row = userDao.editPwd(u_pwd1,u_id,u_pwd);
            if (row>0){
                flag=true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    //添加收货地址
    public  boolean addAddress(String u_id,String a_name,String a_add,String a_tel){
            boolean flag=false;
        try {
            int row = userDao.addAddress(u_id,a_add,a_name,a_tel);
            if (row>0){
                flag=true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  flag;
    }

    //修改已保存的收货地址
    public boolean updateAddress(String a_name,String a_add,String a_tel,String a_id){
        boolean flag=false;
        try {
            int row = userDao.updateAddress(a_name,a_add,a_tel,a_id);
            if (row>0){
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
    //查询已添加的收货地址
    public List<Map<String,Object>> findAddressById(String u_id){
        List<Map<String,Object>> list = null;
        try {
            list=userDao.findAddressById(u_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  list;
    }
    //删除已添加的收货地址
    public boolean deleteAddress(String a_id){
        boolean flag=false;
        try {
            int row=userDao.deleteAddress(a_id);
            if (row>0){
                flag=true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
    public Map<String,Object> doFindUserById(String id){
        Map<String,Object> map=null;
        try {
            List<Map<String,Object>> list=userDao.findUserById(id);
            if (list!=null&&!list.isEmpty()){
                map=list.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    //管理员查询用户信息
    public List<Map<String,Object>> doMgrFindUser(){
        List<Map<String,Object>> list = null;
        try {
            list= userDao.mgrFindUser();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    //用户信息分页
    public String getPageNum(int pageCount){
        String n="0";
        List<Map<String,Object>> list=userDao.findTitleNum(pageCount);
        if (list.size()>0){
            n=list.get(0).get("a").toString();
        }
        return n;
    }
    public  List<Map<String,Object>> findByPage(String pageIndex,int pageCount){
        List<Map<String,Object>> list=userDao.findByPage(Integer.parseInt(pageIndex),pageCount);
        return list;
    }


}
